/*
 * debug.h
 *
 *  Created on: Sep 6, 2008
 *      Author: pierre
 */

#ifndef DEBUG_HEADER
#define DEBUG_HEADER

#include <stdio.h>
#include "lang/morestddef.h"

#ifdef  __cplusplus
extern "C" {
#endif

extern void debug_setDebugging(op_bool allow);
extern op_bool debug_isDebugging();
extern FILE* debug_getStream();
extern void debug_setStream(FILE*);

#ifdef NDEBUG
	#define DEBUG do  { } while(false)
	#define DEBUG1(a,b) DEBUG
	#define DEBUG2(a,b) DEBUG
	#define DEBUG3(a,b,c) DEBUG
	#define DEBUG4(a,b,c,d) DEBUG
#endif

#ifndef NDEBUG
	#define DEBUG if(debug_isDebugging()) fprintf(debug_getStream(),"[DEBUG]%s:%d:%s\n",__FILE__,__LINE__,__FUNCTION__)
	#define DEBUG1(a) do {\
			if(debug_isDebugging())\
				{\
				fprintf(debug_getStream(),"[DEBUG]%s:%d: ",__FILE__,__LINE__);\
				fprintf(debug_getStream(),a);\
				fputc('\n',debug_getStream()); fflush(debug_getStream());\
				}\
			} while(false)

	#define DEBUG2(a,b) do {\
		if(debug_isDebugging())\
			{\
			fprintf(debug_getStream(),"[DEBUG]%s:%d: ",__FILE__,__LINE__);\
			fprintf(debug_getStream(),a,b);\
			fputc('\n',debug_getStream()); fflush(debug_getStream());\
			}\
		} while(false)

	#define DEBUG3(a,b,c) do {\
		if(debug_isDebugging())\
			{\
			fprintf(debug_getStream(),"[DEBUG]%s:%d: ",__FILE__,__LINE__);\
			fprintf(debug_getStream(),a,b,c);\
			fputc('\n',debug_getStream()); fflush(debug_getStream());\
			}\
		} while(false)

	#define DEBUG4(a,b,c,d) do {\
		if(debug_isDebugging())\
			{\
			fprintf(debug_getStream(),"[DEBUG]%s:%d: ",__FILE__,__LINE__);\
			fprintf(debug_getStream(),a,b,c,d);\
			fputc('\n',debug_getStream()); fflush(debug_getStream());\
			}\
		} while(false)
#endif

#ifdef  __cplusplus
}
#endif

#endif /* DEBUG_H_ */
